﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using FileFolder;
using System.IO;
using XMLDataSerializer;

namespace ViCode_LeVi.Controls
{
    public class VMListView: ListView
    {
        public VMListView():base()
        {
            if (this.Style == null)
            {
                this.Style = Application.Current.Resources[typeof(ListView)] as Style;
            }
            this.Loaded += new RoutedEventHandler(VMListView_Loaded);
            this.Unloaded += new RoutedEventHandler(VMListView_Unloaded);        
        }
        
        void VMListView_Unloaded(object sender, RoutedEventArgs e)
        {
            ListView lvw = this;
            string path = GetListViewPathConfig();
            try
            {
                if (File.Exists(path))
                    File.Delete(path);
            }
            catch { }
            //Get width
            List<double> listSize = new List<double>();
            GridView gv = (GridView)lvw.View;
            foreach (var c in gv.Columns)
                if (!c.IsColumnOrder())
                    listSize.Add(c.ActualWidth);
            ListViewConfig config = new ListViewConfig();
            config.Widths = listSize.ToArray();
            try
            {
                File.Delete(path);
            }
            catch
            { }
            bool bResult = XMLData<ListViewConfig>.SaveEntity(path, config);
            if (bResult == false)
            {
                Console.WriteLine("Không lưu được cấu hình ListView vào " + path);
            }             
        }

        void VMListView_Loaded(object sender, RoutedEventArgs e)
        {
            ListView lvw = this;
            string path = GetListViewPathConfig();
            ListViewConfig config = XMLData<ListViewConfig>.GetEntity(path);
            if (config == null)
                return;
            //Set width
            GridView gv = (GridView)lvw.View;
            if (config.Widths != null && gv != null)
            {
                int i = 0;
                //Bỏ đi cột STT đầu tiên
                int iExt = 0;
                if (gv.Columns.Count > 0)
                    if (gv.Columns[0].IsColumnOrder())
                    {
                        iExt = 1;
                    }
                while ((i - iExt) < config.Widths.Length && i < gv.Columns.Count)
                {
                    if (i - iExt > 0)
                        gv.Columns[i].Width = config.Widths[i - iExt];
                    i++;
                }
            }
        }
        string GetListViewPathConfig()
        {
            string sFolder = "ListViewConfig";
            string file = SaveSettingKey;           
            string sPathFlder = FileFolder.AppInfo.AppDirectory + "\\" + sFolder;
            string path = sPathFlder + "\\" + file + ".lvwConfig";
            path = sPathFlder + "\\" + file + ".lvwConfig";
            FolderHelper.EnsureExistFolder(sPathFlder);
            if (!File.Exists(path))
                File.Create(path);
            return path;
        }
        public bool IsSaveSetting { get; set; }
        public string SaveSettingKey { get; set; }
    }
}
